package com.feicheng.iou.controller;


import com.alibaba.excel.EasyExcel;
import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.constant.SessionConstant;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.Student;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.pojo.ResourcePOJO;
import com.feicheng.iou.pojo.excelHandle.ExcelPutModel;
import com.feicheng.iou.pojo.login.LoginReturnPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.ResourceRecordService;
import com.feicheng.iou.service.ResourceService;
import com.feicheng.iou.service.ExcelExportMapperService;
import com.feicheng.iou.service.StudentService;
import com.feicheng.iou.util.IdempotentUtils;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@RestController
@RequestMapping("/iou/resource")
@Slf4j
@Setter
@CrossOrigin
public class ResourceController {

    @Autowired
    private ResourceService resourceService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ResourceRecordService resourceRecordService;

    @Autowired
    private HttpSession session;

    /**
     * @Author luoxin
     * @Description //添加线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PostMapping
    public ResultInfo addResource( ResourcePOJO resourcePOJO) {   //添加线索
        log.info("addResource方法进入，参数为: " + resourcePOJO);
        //判断请求是否重复
        if (!IdempotentUtils.judge(resourcePOJO, this.getClass())){
            throw  new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004,"线索添加"));
        }

        // 参数校验是否为空
        if (resourcePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索添加业务逻辑
        return resourceService.insert(resourcePOJO);
    }

    /**
     * @Author luoxin
     * @Description //修改线索的接口
     * @Date 下午5:34 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PutMapping
    public ResultInfo updateReource(ResourcePOJO resourcePOJO) {  //修改线索
        log.info("updateReource方法进入，参数为: " + resourcePOJO);
        // 参数校验是否为空
        if (resourcePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索修改业务逻辑
        return resourceService.update(resourcePOJO);

    }

    /**
     * @Author luoxin
     * @Description //查询线索列表的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    //ResourcePOJO resourcePOJO
    @GetMapping("/list")
    public ResultInfo selectResourceList(ResourcePOJO resourcePOJO, HttpServletRequest request) {  //查询线索列表
        log.info("selectResourceList方法进入，参数为: " + resourcePOJO);

        // 参数校验是否为空
        if (resourcePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索列表查询业务逻辑
         return resourceService.listAll(resourcePOJO,request);
    }

    /**
     * @Author luoxin
     * @Description //查询单个线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param id
     * @return ResultInfo
     */
    @GetMapping("/info/{id}")
    public ResultInfo selectResourceInfo(@PathVariable("id") Integer id) {  //查询单个线索
        log.info("selectResourceInfo方法进入，参数为: " + id);

        // 参数校验是否为空
        if (id == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }
        //调用ResourceService实现线索单个查询业务逻辑
        return resourceService.selectinfo(id);
    }


    /**
     * @Author luoxin
     * @Description //删除线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @DeleteMapping
    public ResultInfo deleteResource( ResourcePOJO resourcePOJO) {  //删除线索
        log.info("deleteResource方法进入，参数为: " + resourcePOJO);

//         参数校验是否为空
        if (resourcePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索添加业务逻辑
        return resourceService.delete(resourcePOJO);
    }

    /**
     * @Author luoxin
     * @Description //分配线索的接口
     * @Date 下午5:32 2021/7/13
     * @Param ResourcePOJO
     * @return ResultInfo
     */
    @PutMapping("/autor")
    public ResultInfo updateAutor(ResourcePOJO resourcePOJO) {  //分配线索
        log.info("addResource方法进入，参数为: " + resourcePOJO);

        //参数校验是否为空
        if (resourcePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索分配业务逻辑
        return resourceService.updateAutor(resourcePOJO);
    }
    /**
     * @Author xuxinjie
     * @Description 根据学生name 查重学生信息接口
     * @Date 2021-7-3 16:08
     * @param name 学生姓名
     * @return ResultInfo
     */
    @GetMapping("/duplication/{name}")
    public ResultInfo duplicateResource(@PathVariable("name") String name){
        //调用selectStudentByName方法查询线索信息
        log.info("ResourceController的selectResource方法进入");

        if (null == name){
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        return resourceService.selectResourceByName(name);
    }
    /**
     * @Author luoxin
     * @Description 放弃线索的接口
     * @Date 1:56 下午 2021/7/26
     * @param resourcePOJO
     * @return ResultInfo
     */
    @PutMapping("/abandon")
    public ResultInfo abandonResource( ResourcePOJO resourcePOJO) { //放弃线索
        log.info("abandon方法进入，参数为: " + resourcePOJO);

        //参数校验是否为空
        if (resourcePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现线索分配业务逻辑
        return resourceService.abandonResource(resourcePOJO);
    }
    /**
     * @Author luoxin
     * @Description excel导出的接口
     * @Date 1:09 下午 2021/8/5
     * @param response
     * @return
     */
    @GetMapping("/excel/out")
    public void excelOut(HttpServletResponse response) throws IOException {
        log.info("excelOut方法进入" );

        //调用ResourceService实现线索分配业务逻辑
        resourceService.excelOut(response);
    }
    /**
     * @Author luoxin
     * @Description excel导入的接口
     * @Date 1:12 下午 2021/8/5
     * @param file request
     * @return
     */
    @PostMapping("/excel/put")
    public ResultInfo excelPut(MultipartFile file,HttpServletRequest request) throws IOException {
        log.info("excelPut方法进入");

        EasyExcel.read(file.getInputStream(), ExcelPutModel.class, new ExcelExportMapperService(resourceService,studentService,resourceRecordService,request)).sheet().doRead();

        return ResultInfo.success();
    }

    @PostMapping("/transfer")
    public ResultInfo transferResource( ResourcePOJO resourcePOJO) {  //放弃线索
        log.info("transferResource方法进入，参数为: " + resourcePOJO);

        //判断请求是否重复
        if (!IdempotentUtils.judge(resourcePOJO, this.getClass())){
            throw  new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004,"线索添加"));
        }
        //参数校验是否为空
        if (resourcePOJO == null) {
            throw new SystemException(ReturnCode.PARAM_NOT_NULL);
        }

        //调用ResourceService实现转线索业务逻辑
        return resourceService.transferRessource(resourcePOJO);
    }
}


